<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book reacjs开发 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>redux | reacjs开发</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../005day/section03.html" />
    
    
    <link rel="prev" href="../005day/section01.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="5.2"
        data-chapter-title="redux"
        data-filepath="005day/section02.md"
        data-basepath=".."
        data-revision="Sun Feb 16 2020 11:31:01 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="001day/index.html">
            
                
                    <a href="../001day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        react入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="001day/section01.html">
            
                
                    <a href="../001day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        react介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="001day/section02.html">
            
                
                    <a href="../001day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        快速开始
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="001day/section03.html">
            
                
                    <a href="../001day/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        JSX语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="001day/section04.html">
            
                
                    <a href="../001day/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        函数组件和参数props
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="001day/section05.html">
            
                
                    <a href="../001day/section05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        ES6语法之类和类的继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="001day/section06.html">
            
                
                    <a href="../001day/section06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        类组件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="001day/section07.html">
            
                
                    <a href="../001day/section07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        绑定事件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="001day/section08.html">
            
                
                    <a href="../001day/section08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        状态(state)
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="002day/index.html">
            
                
                    <a href="../002day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        react进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="002day/section01.html">
            
                
                    <a href="../002day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        新增数组方法、属性表达式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="002day/section02.html">
            
                
                    <a href="../002day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        列表渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="002day/section03.html">
            
                
                    <a href="../002day/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        条件渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="002day/section04.html">
            
                
                    <a href="../002day/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        表单数据绑定
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="002day/section05.html">
            
                
                    <a href="../002day/section05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        Refs操作元素，文件上传
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="002day/section06.html">
            
                
                    <a href="../002day/section06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        生命周期方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="003day/index.html">
            
                
                    <a href="../003day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        脚手架开发
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="003day/section01.html">
            
                
                    <a href="../003day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        yarn的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="003day/section02.html">
            
                
                    <a href="../003day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        脚手架开发
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="004day/index.html">
            
                
                    <a href="../004day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        react高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="004day/section01.html">
            
                
                    <a href="../004day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        js模块化开发
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="004day/section02.html">
            
                
                    <a href="../004day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        数据传递
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="004day/section03.html">
            
                
                    <a href="../004day/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        拆分组件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="004day/section04.html">
            
                
                    <a href="../004day/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        组件children属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="004day/section05.html">
            
                
                    <a href="../004day/section05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        props校验
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="004day/section06.html">
            
                
                    <a href="../004day/section06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        props默认值
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="004day/section07.html">
            
                
                    <a href="../004day/section07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        render-props模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="004day/section08.html">
            
                
                    <a href="../004day/section08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        高阶组件(HOC)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="004day/section09.html">
            
                
                    <a href="../004day/section09.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        组件更新机制及性能优化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="004day/section10.html">
            
                
                    <a href="../004day/section10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.10.</b>
                        
                        虚拟dom及diff算法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="005day/index.html">
            
                
                    <a href="../005day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        react深入
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="005day/section01.html">
            
                
                    <a href="../005day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        react路由
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5.2" data-path="005day/section02.html">
            
                
                    <a href="../005day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="005day/section03.html">
            
                
                    <a href="../005day/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        react-redux
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >reacjs开发</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="redux">Redux</h2>
<p>react&#x4E2D;&#x7684;&#x6570;&#x636E;&#x4FDD;&#x5B58;&#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x7EC4;&#x4EF6;&#x4E4B;&#x95F4;&#x4F20;&#x9012;&#x6570;&#x636E;&#xFF0C;&#x5BF9;&#x4E8E;&#x7B80;&#x5355;&#x7684;&#x7CFB;&#x7EDF;&#x662F;&#x6CA1;&#x90A3;&#x4E48;&#x9EBB;&#x70E6;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5BF9;&#x4E8E;&#x590D;&#x6742;&#x7684;&#x7CFB;&#x7EDF;&#xFF0C;&#x7EC4;&#x4EF6;&#x4E4B;&#x95F4;&#x4F20;&#x9012;&#x6570;&#x636E;&#x5C31;&#x53D8;&#x5F97;&#x590D;&#x6742;&#x800C;&#x4E14;&#x96BE;&#x4EE5;&#x7EF4;&#x62A4;&#xFF0C;&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#x662F;&#x5C06;&#x6570;&#x636E;&#x653E;&#x5728;&#x4E00;&#x4E2A;&#x5730;&#x65B9;&#x8FDB;&#x884C;&#x96C6;&#x4E2D;&#x7BA1;&#x7406;&#xFF0C;&#x6240;&#x6709;&#x7684;&#x7EC4;&#x4EF6;&#x90FD;&#x5171;&#x4EAB;&#x540C;&#x4E00;&#x4E2A;&#x5730;&#x65B9;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x7B80;&#x5316;&#x6570;&#x636E;&#x7684;&#x64CD;&#x4F5C;&#x6D41;&#x7A0B;&#xFF0C;&#x800C;Redux&#x5C31;&#x662F;&#x5B9E;&#x73B0;&#x8FD9;&#x79CD;&#x6570;&#x636E;&#x7BA1;&#x7406;&#x65B9;&#x5F0F;&#x7684;&#x6A21;&#x5757;&#x3002;</p>
<h4 id="redux&#x57FA;&#x672C;&#x539F;&#x7406;">Redux&#x57FA;&#x672C;&#x539F;&#x7406;</h4>
<p><img src="images/redux.png" alt="redux&#x5DE5;&#x4F5C;&#x539F;&#x7406;&#x56FE;"><br>&#x4E0A;&#x9762;&#x662F;redux&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;&#x56FE;&#xFF0C;redux&#x7684;&#x529F;&#x80FD;&#x53EF;&#x4EE5;&#x5206;&#x6210;&#x56FE;&#x7247;&#x7684;&#x4E0A;&#x9762;&#x4E09;&#x5757;&#xFF0C;&#x4E0B;&#x9762;&#x84DD;&#x8272;&#x5757;&#x7684;&#x662F;&#x7EC4;&#x4EF6;&#xFF0C;&#x4EE5;&#x4E0B;&#x5BF9;redux&#x7684;&#x4E09;&#x5757;&#x529F;&#x80FD;&#x505A;&#x4E00;&#x4E2A;&#x8BF4;&#x660E;&#xFF1A;</p>
<ul>
<li>Store &#x6570;&#x636E;&#x8C03;&#x63A7;&#x4E2D;&#x5FC3;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;&#x662F;&#x6570;&#x636E;&#x4ED3;&#x5E93;&#x7684;&#x7BA1;&#x7406;&#x5458;&#xFF0C;&#x7EC4;&#x4EF6;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B83;&#x5F97;&#x5230;&#x6570;&#x636E;</li>
<li>Action Creators &#x7EC4;&#x4EF6;&#x53D1;&#x8D77;&#x7684;&#x6570;&#x636E;&#x53D8;&#x66F4;&#x9700;&#x6C42;&#xFF0C;&#x5B83;&#x76F8;&#x5F53;&#x4E8E;&#x662F;&#x4ED3;&#x5E93;&#x7684;&#x53D8;&#x66F4;&#x5DE5;&#x4F5C;&#x5DE5;&#x5355;&#xFF0C;&#x7BA1;&#x7406;&#x5458;&#x4F1A;&#x62FF;&#x5230;&#x5DE5;&#x5355;&#xFF0C;&#x7136;&#x540E;&#x53BB;&#x4ED3;&#x5E93;&#x53D8;&#x66F4;&#x6570;&#x636E;</li>
<li>Reducers &#x6570;&#x636E;&#x4ED3;&#x5E93;&#xFF0C;&#x6570;&#x636E;&#x7684;&#x5B58;&#x50A8;&#x548C;&#x53D8;&#x66F4;&#x5728;&#x8FD9;&#x91CC;&#x5B8C;&#x6210;&#xFF0C;&#x5B8C;&#x6210;&#x4E4B;&#x540E;&#xFF0C;&#x4ED3;&#x5E93;&#x4F1A;&#x7ED9;Store&#x8FD4;&#x56DE;&#x6700;&#x65B0;&#x7684;&#x6570;&#x636E;&#x3002;</li>
</ul>
<h4 id="&#x4EE3;&#x7801;&#x5B9E;&#x73B0;">&#x4EE3;&#x7801;&#x5B9E;&#x73B0;</h4>
<p>&#x9996;&#x5148;&#x8981;&#x5B89;&#x88C5;&#x8FD9;&#x4E2A;&#x6A21;&#x5757;&#xFF1A;</p>
<pre><code>yarn add redux
</code></pre><p>&#x7136;&#x540E;&#x5728;src&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;store&#x6587;&#x4EF6;&#x5939;&#xFF0C;&#x91CC;&#x9762;&#x5355;&#x72EC;&#x653E;&#x7F6E;redux&#x76F8;&#x5173;&#x4EE3;&#x7801;&#xFF0C;&#x9996;&#x5148;&#x5728;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;index.js&#x6587;&#x4EF6;&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x7F16;&#x5199;&#x4E0A;&#x9762;&#x56FE;&#x7247;&#x4E2D;&#x7684;Store&#x6A21;&#x5757;&#x5BF9;&#x5E94;&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5BFC;&#x5165;&#x521B;&#x5EFA;store&#x7684;&#x7684;&#x5DE5;&#x5382;&#x51FD;&#x6570;</span>
<span class="hljs-keyword">import</span> { createStore } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-comment">// &#x5BFC;&#x5165;&#x7B49;&#x4F1A;&#x8981;&#x521B;&#x5EFA;&#x7684;reducer&#x6587;&#x4EF6;</span>
<span class="hljs-keyword">import</span> reducer <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducer&apos;</span>

// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;store
let store = createStore(reducer);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> store;
</code></pre>
<p>&#x63A5;&#x7740;&#x5728;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;reducer.js&#x6587;&#x4EF6;&#xFF0C;&#x6587;&#x4EF6;&#x91CC;&#x9762;&#x7F16;&#x5199;&#x4E0A;&#x9762;&#x56FE;&#x7247;&#x4E2D;&#x7684;Reducers&#x5BF9;&#x5E94;&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> defaultState = {
    <span class="hljs-string">&quot;aList&quot;</span>:[],
    <span class="hljs-string">&quot;sTodo&quot;</span>:<span class="hljs-string">&quot;&quot;</span>
};
<span class="hljs-comment">// state&#x53C2;&#x6570;&#x8D77;&#x59CB;&#x5B58;&#x653E;&#x7684;&#x662F;&#x539F;&#x59CB;&#x6570;&#x636E;</span>
<span class="hljs-comment">// action&#x53C2;&#x6570;&#x662F;&#x7531;store&#x4F20;&#x9012;&#x8FDB;&#x6765;&#x7684;&#x6570;&#x636E;&#x53D8;&#x66F4;&#xFF0C;&#x5B83;&#x5176;&#x5B9E;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;</span>
<span class="hljs-keyword">let</span> reducer = (state=defaultState,action)=&gt;{
    <span class="hljs-keyword">return</span> state;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> reducer;
</code></pre>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x7EC4;&#x4EF6;&#x4E2D;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x6570;&#x636E;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5BFC;&#x5165;store&#x6587;&#x4EF6;&#x5939;&#x4E0B;&#x9762;&#x7684;index.js&#x6587;&#x4EF6;&#xFF0C;&#x4E0B;&#x9762;&#x662F;&#x7B80;&#x5199;&#x65B9;&#x5F0F;</span>
<span class="hljs-keyword">import</span> store <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./store&apos;</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Todolist</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    constructor(props){
        <span class="hljs-keyword">super</span>(props);
        <span class="hljs-comment">// &#x6267;&#x884C;store.getState&#x65B9;&#x6CD5;&#x83B7;&#x53D6;store&#x4E2D;&#x7684;&#x6570;&#x636E;</span>
        <span class="hljs-keyword">this</span>.state = store.getState();
    }
    ......
</code></pre>
<p>&#x8981;&#x53D8;&#x66F4;&#x6570;&#x636E;&#xFF0C;&#x5C31;&#x5728;&#x7EC4;&#x4EF6;&#x7684;&#x65B9;&#x6CD5;&#x4E2D;&#x7F16;&#x5199;Action Creators&#x5BF9;&#x5E94;&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x6570;&#x636E;&#x53D8;&#x66F4;&#x7684;action&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;</span>
<span class="hljs-comment">// &#x901A;&#x8FC7;store.dispatch&#x65B9;&#x6CD5;&#x5C06;&#x6570;&#x636E;&#x53D8;&#x66F4;&#x4F20;&#x7ED9;Store</span>
fnChange=(e)=&gt;{
    <span class="hljs-keyword">const</span> action = {
        type:<span class="hljs-string">&apos;change_val&apos;</span>,
        value:e.target.value
    }
    store.dispatch(action);
}
</code></pre>
<p>Store&#x4F1A;&#x81EA;&#x52A8;&#x5C06;&#x53D8;&#x66F4;&#x4F20;&#x7ED9;Reducers&#xFF0C;&#x63A5;&#x7740;&#x5728;reducer.js&#x4E2D;&#x7F16;&#x5199;&#x5BF9;&#x5E94;&#x53D8;&#x66F4;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> reducer = (state=defaultState,action)=&gt;{
    <span class="hljs-keyword">if</span>(action.type==<span class="hljs-string">&apos;change_val&apos;</span>){
        <span class="hljs-keyword">const</span> oNewState = <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-built_in">JSON</span>.stringify(state));
        oNewState.sTodo = action.value;
        <span class="hljs-keyword">return</span> oNewState;
    }
    <span class="hljs-keyword">return</span> state;
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> reducer;
</code></pre>
<p>&#x6570;&#x636E;&#x53D8;&#x66F4;&#x540E;&#xFF0C;Store&#x62FF;&#x5230;&#x6700;&#x65B0;&#x6570;&#x636E;&#xFF0C;&#x9700;&#x8981;&#x901A;&#x77E5;&#x7EC4;&#x4EF6;&#x66F4;&#x65B0;state&#x91CC;&#x9762;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x7EC4;&#x4EF6;&#x9700;&#x8981;&#x8BA2;&#x9605;Store&#x7684;&#x901A;&#x77E5;&#xFF0C;&#x4E00;&#x65E6;&#x6536;&#x5230;&#x901A;&#x77E5;&#xFF0C;&#x5C31;&#x8BA9;&#x7EC4;&#x4EF6;&#x91CD;&#x65B0;&#x83B7;&#x53D6;Store&#x4E2D;&#x7684;&#x6570;&#x636E;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Todolist</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    constructor(props){
        <span class="hljs-keyword">super</span>(props);
        <span class="hljs-keyword">this</span>.state = store.getState();
        <span class="hljs-comment">// &#x8BA2;&#x9605;store&#x4E2D;&#x7684;&#x901A;&#x77E5;&#xFF0C;&#x6536;&#x5230;&#x901A;&#x77E5;&#xFF0C;&#x5C31;&#x89E6;&#x53D1;fnStoreChange&#x65B9;&#x6CD5;&#x91CD;&#x65B0;&#x6216;&#x8005;&#x6570;&#x636E;</span>
        <span class="hljs-comment">// &#x8BA2;&#x9605;&#x901A;&#x77E5;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x89E3;&#x9664;&#x8BA2;&#x9605;</span>
        <span class="hljs-keyword">this</span>.unsubscribe = store.subscribe(<span class="hljs-keyword">this</span>.fnStoreChange);
    }
    <span class="hljs-comment">// &#x5411;store&#x83B7;&#x53D6;&#x6570;&#x636E;</span>
    fnStoreChange=()=&gt;{
        <span class="hljs-keyword">this</span>.setState(store.getState());
    }
</code></pre>
<p>&#x6570;&#x636E;&#x5982;&#x679C;&#x5B58;&#x653E;&#x5728;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x7EC4;&#x4EF6;&#x521D;&#x59CB;&#x5316;&#x65F6;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;ajax&#x5C06;&#x8FDC;&#x7A0B;&#x6570;&#x636E;&#x8BFB;&#x53D6;&#x56DE;&#x6765;&#x5B58;&#x5165;redux&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5728;&#x751F;&#x547D;&#x5468;&#x671F;&#x51FD;&#x6570;&#x4E2D;&#x81EA;&#x52A8;&#x53BB;&#x83B7;&#x53D6;&#x8FDC;&#x7A0B;&#x6570;&#x636E;</span>
componentDidMount(){
        axios.get(<span class="hljs-string">&apos;./data.json&apos;</span>).then(res=&gt;{
            store.dispatch({
                type:<span class="hljs-string">&apos;init_data&apos;</span>,
                value:res.data
            })
        })
    }
</code></pre>
<p>&#x5728;reducer.js&#x4E2D;&#x6DFB;&#x52A0;&#x5BF9;&#x5E94;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(action.type==<span class="hljs-string">&apos;init_data&apos;</span>){       
    <span class="hljs-keyword">return</span> action.value;
}
</code></pre>
<h4 id="todolist-&#x4EE3;&#x7801;&#x793A;&#x4F8B;">todolist &#x4EE3;&#x7801;&#x793A;&#x4F8B;</h4>
<p>&#x7EC4;&#x4EF6;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-javascript">import React, {Component} from &apos;react&apos;;
import store from &apos;./store&apos;;
import {CHANGE_VAL,ADD_LIST,DEL_LIST,INIT_DATA} from &apos;./store/actionType&apos;;
import axios from &apos;axios&apos;

class Todolist extends Component{
    constructor(props){
        super(props);
        //console.log(store.getState());
        this.state = store.getState();
        this.unsubscribe = store.subscribe(this.fnStoreChange);

    }
    fnChange=(e)=&gt;{
        const action = {
            type:CHANGE_VAL,
            value:e.target.value
        }
        store.dispatch(action);
    }

    fnAdd=()=&gt;{
        const action = {
            type:ADD_LIST    
        }
        store.dispatch(action);
    }

    fnDel=(i)=&gt;{
       store.dispatch({
           type:DEL_LIST,
           value:i
       })
    }

    fnStoreChange=()=&gt;{
        this.setState(store.getState());
    }

    componentDidMount(){
        axios.get(&apos;./data.json&apos;).then(res=&gt;{
            //console.log(res.data);
            store.dispatch({
                type:INIT_DATA,
                value:res.data
            })
        })
    }

    componentWillUnmount(){
        // &#x9700;&#x8981;&#x89E3;&#x9664;&#x8BA2;&#x9605;&#xFF0C;&#x5426;&#x5219;&#x5BB9;&#x6613;&#x5185;&#x5B58;&#x6EA2;&#x51FA;
        this.unsubscribe();
    }

    render(){
        return (
            &lt;div className=&quot;list_con&quot;&gt;
                &lt;h2&gt;To do list&lt;/h2&gt;
                &lt;input type=&quot;text&quot;  value={this.state.sTodo} onChange={this.fnChange} className=&quot;inputtxt&quot; /&gt;
                &lt;input type=&quot;button&quot; value=&quot;&#x589E;&#x52A0;&quot; onClick={this.fnAdd} className=&quot;inputbtn&quot; /&gt;

                &lt;ul id=&quot;list&quot; className=&quot;list&quot;&gt;
                    {
                        this.state.aList.map((item,index)=&gt;
                             &lt;li key={index}&gt;&lt;span&gt;{index+1}&#x3001;{item}&lt;/span&gt;&lt;a href=&quot;javascript:;&quot; className=&quot;del&quot; onClick={()=&gt;{ this.fnDel(index) }}&gt;&#x5220;&#x9664;&lt;/a&gt;&lt;/li&gt;
                        )
                    }                   
                &lt;/ul&gt;
            &lt;/div&gt;            
        )
    }
}

export default Todolist;
</code></pre>
<p>store&#x6587;&#x4EF6;&#x5939;(redux&#x529F;&#x80FD;)&#x4E2D;&#x7684;index.js&#x5BF9;&#x5E94;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { createStore } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> reducer <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducer&apos;</span>

let store = createStore(reducer);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> store;
</code></pre>
<p>store&#x6587;&#x4EF6;&#x5939;(redux&#x529F;&#x80FD;)&#x4E2D;&#x7684;reducer.js&#x5BF9;&#x5E94;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {CHANGE_VAL,ADD_LIST,DEL_LIST,INIT_DATA} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./actionType&apos;</span>

const defaultState = {
    <span class="hljs-string">&quot;aList&quot;</span>:[],
    <span class="hljs-string">&quot;sTodo&quot;</span>:<span class="hljs-string">&quot;&quot;</span>
};

<span class="hljs-keyword">let</span> reducer = (state=defaultState,action)=&gt;{
    <span class="hljs-keyword">if</span>(action.type==CHANGE_VAL){
        <span class="hljs-keyword">const</span> oNewState = <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-built_in">JSON</span>.stringify(state));
        oNewState.sTodo = action.value;
        <span class="hljs-keyword">return</span> oNewState;
    }

    <span class="hljs-keyword">if</span>(action.type==ADD_LIST){
        <span class="hljs-keyword">const</span> oNewState = <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-built_in">JSON</span>.stringify(state));
        oNewState.aList.push(oNewState.sTodo);
        oNewState.sTodo = <span class="hljs-string">&apos;&apos;</span>;
        <span class="hljs-keyword">return</span> oNewState;
    }

    <span class="hljs-keyword">if</span>(action.type==DEL_LIST){
        <span class="hljs-keyword">const</span> oNewState = <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-built_in">JSON</span>.stringify(state));
        oNewState.aList.splice(action.value,<span class="hljs-number">1</span>)
        <span class="hljs-keyword">return</span> oNewState;
    }

    <span class="hljs-keyword">if</span>(action.type==INIT_DATA){       
        <span class="hljs-keyword">return</span> action.value;
    }

    <span class="hljs-keyword">return</span> state;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> reducer;
</code></pre>
<p>&#x4E3A;&#x4E86;&#x9632;&#x6B62;action&#x4E2D;&#x7684;type&#x5BB9;&#x6613;&#x5199;&#x9519;&#xFF0C;&#x5C06;type&#x4E2D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x5B58;&#x4E3A;&#x5E38;&#x91CF;&#xFF0C;&#x72EC;&#x7ACB;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x4E3A;&#xFF1A;actionType.js&#xFF0C;&#x6587;&#x4EF6;&#x4EE3;&#x7801;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> CHANGE_VAL = <span class="hljs-string">&apos;change_val&apos;</span>;
<span class="hljs-keyword">const</span> ADD_LIST = <span class="hljs-string">&apos;add_list&apos;</span>;
<span class="hljs-keyword">const</span> DEL_LIST = <span class="hljs-string">&apos;del_list&apos;</span>;
<span class="hljs-keyword">const</span> INIT_DATA = <span class="hljs-string">&apos;init_data&apos;</span>;

<span class="hljs-keyword">export</span> {CHANGE_VAL,ADD_LIST,DEL_LIST,INIT_DATA};
</code></pre>
<p>&#x8FDC;&#x7A0B;&#x6570;&#x636E;&#x4F7F;&#x7528;&#x4E86;&#x4E00;&#x4E2A;json&#x6587;&#x4EF6;&#x6A21;&#x62DF;&#xFF0C;&#x6587;&#x4EF6;&#x540D;&#x4E3A; data.json ,&#x6587;&#x4EF6;&#x4EE3;&#x7801;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;aList&quot;</span>:[<span class="hljs-string">&quot;&#x5B66;&#x4E60;html&quot;</span>,<span class="hljs-string">&quot;&#x5B66;&#x4E60;css&quot;</span>,<span class="hljs-string">&quot;&#x5B66;&#x4E60;JavaScript&quot;</span>,<span class="hljs-string">&quot;&#x5B66;&#x4E60;vue&quot;</span>,<span class="hljs-string">&quot;&#x5B66;&#x4E60;react&quot;</span>],
    <span class="hljs-string">&quot;sTodo&quot;</span>:<span class="hljs-string">&quot;&quot;</span>
}
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../005day/section01.html" class="navigation navigation-prev " aria-label="Previous page: react路由"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../005day/section03.html" class="navigation navigation-next " aria-label="Next page: react-redux"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/fontsettings.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book reacjs开发 -->
</html>
